/****************************************************
	文件：ProtoMgr.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/5/1 17:26:6
	功能：协议管理类，处理所有网络协议的收发
*****************************************************/

using System;
using UnityEngine;
using HTServer.Proto;

namespace HTClient.Net
{
    public class ProtoMgr
    {
        /// <summary>
        /// 请求秘钥（成功连接服务器后发送的第一个请求）
        /// </summary>
		public static void SecretRequest()
        {
            MsgSecret msg = new MsgSecret();
            NetMgr.Instance.SendMsg(msg);
            //请求得到回应之后的监听回调
            NetMgr.Instance.AddProtoListenner(ProtoEnum.MsgSecret, m =>
             {
                 MsgSecret s = (MsgSecret)m;
                 NetMgr.Instance.SetSecretKey(s.SecretKey);
                 Debug.Log("请求到的秘钥：" + s.SecretKey);
             });
        }
        /// <summary>
        /// 注册请求
        /// </summary>
        public static void RegisterRequest(RegisterType registerType, string userName, string pwd, string code, Action<RegisterResult, string> cb)
        {
            MsgRegister msg = new MsgRegister();
            msg.RegisterType = registerType;
            msg.Username = userName;
            msg.Password = pwd;
            msg.Code = code;//验证码
            NetMgr.Instance.SendMsg(msg);
            NetMgr.Instance.AddProtoListenner(ProtoEnum.MsgRegister, m =>
            {
                MsgRegister rec = (MsgRegister)m;
                cb?.Invoke(rec.Result, rec.NewToken);
            });
        }
        /// <summary>
        /// 登录请求
        /// </summary>
        public static void LoginRequest(LoginType loginType, string userName, string pwd, string token, Action<LoginResult, string> cb)
        {
            MsgLogin msg = new MsgLogin();
            msg.LoginType = loginType;
            msg.Username = userName;
            msg.Password = pwd;
            msg.Token = token;//自动登录使用的
            NetMgr.Instance.SendMsg(msg);
            NetMgr.Instance.AddProtoListenner(ProtoEnum.MsgLogin, m =>
            {
                MsgLogin rec = (MsgLogin)m;
                cb?.Invoke(rec.Result, rec.NewToken);
            });
        }


        #region Test
        /// <summary>
        /// 分包测试请求
        /// </summary>
        public static void TestSubcontractRequest()
        {
            MsgTestSubcontract msg = new MsgTestSubcontract();
            //一次性发送大量数据，底层进行分包优化：
            msg.ReqContent = "分包测试，来自客户端的信息：huskyTGame.github.io+asdagfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsfasfadsadagfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhssfagfagaehyiutoyhngjluopujjgguutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsfasfadsadagfagutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsfasfadsadagfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhssfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxnggfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsdhwqwnjfkrhvcbxcnfzvhsgfagaehyiugfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhssfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxnggfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsdhwqwnjfkrhvcbxcnfzvhsgfagaehyiugfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxtoutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsfasfadsadagfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhssfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxnggfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsdhwqwnjfkrhvcbxcnfzvhsgfagaehyiugfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsfasfadsadagfagaehyiutoyhutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsfasfadsadagfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhssfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxnggfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsdhwqwnjfkrhvcbxcnfzvhsgfagaehyiugfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhssfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxnggfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsdhwqwnjfkrhvcbxcnfzvhsgfagaehyiugfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxsdvquosdbnmzxnggfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsdhwqwnjfkrhvcbxcnfzvhsgfagaehyiugfagfagaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhsgaehyiutoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhstoyhngjluopujjggsdvquosdbnmzxngdhwqwnjfkrhvcbxcnfzvhs";
            NetMgr.Instance.SendMsg(msg);
            //请求得到回应之后的监听回调
            NetMgr.Instance.AddProtoListenner(ProtoEnum.MsgTestSubcontract, m =>
            {
                MsgTestSubcontract s = (MsgTestSubcontract)m;
                Debug.Log("分包测试，接收来自服务端的信息：\n" + s.RecContent);
            });
        }
        /// <summary>
        /// 粘包测试请求
        /// </summary>
        public static void TestStickRequest()
        {
            MsgTestStick msg = new MsgTestStick();
            msg.ReqContent = "粘包测试，来自客户端的信息";
            //发送多次小数据，底层进行粘包优化：
            NetMgr.Instance.SendMsg(msg);
            NetMgr.Instance.SendMsg(msg);
            NetMgr.Instance.SendMsg(msg);
            NetMgr.Instance.SendMsg(msg);
            NetMgr.Instance.SendMsg(msg);
            NetMgr.Instance.SendMsg(msg);
            //请求得到回应之后的监听回调
            NetMgr.Instance.AddProtoListenner(ProtoEnum.MsgTestStick, m =>
            {
                MsgTestStick s = (MsgTestStick)m;
                Debug.Log("粘包测试，接收来自服务端的信息：\n" + s.RecContent);
            });
        }
        #endregion
    }
}
